<?php if (!defined('BASEPATH')) exit('Access Deny.');
abstract class Db
{
    public static $_db = NULL;
    public static $_type = array('mysql' => 'Mysql', 'mysqli' => 'Mysqli');
    
    protected $hostname;
    protected $username;
    protected $password;
    protected $database;
    protected $port;
    protected $type;
    protected $p_connect;
    
    protected $link;
    
    public function __construct() {}
    
    public static function &init($db_key = 'default')
    {
        $dsn_key = array('hostname', 'username', 'password', 'database', 'port', 'type', 'p_connect');
        try
        {
            $settings = load_config('database');
        }
        catch (Exception $e)
        {
            echo $e->getMessage();
        }
        
        $dsn = array();
        if (isset($settings[$db_key]))
        {
            try
            {
                foreach ($dsn_key as $key)
                {
                    if (!isset($settings[$db_key][$key]))
                    {
                        throw new Db_Exception("Database settings error.");
                    }
                    
                    $dsn[$key] = $settings[$db_key][$key];
                }
                
                $db_type = $settings[$db_key]['type'];
                if (!isset(self::$_type[$db_type]))
                {
                    throw new Db_Exception("can't support your database type");
                }
                
                $classname = 'Db_' . self::$_type[$db_type];
                self::$_db[$db_key] = new $classname($dsn, $db_key);
            }
            catch (Db_Exception $e)
            {
                echo $e->getError();
            }
            
        }
        
        return self::$_db[$db_key];
    }
    
    public abstract function connect();
    /*
    public abstract function close();
    public abstract function query();
    public abstract function insert();
    public abstract function update();
    public abstract function delete();
    public abstract function count();
    */
}